/*
 * 文件名: YearMonthDayAmPmDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-28
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.BaseDialog;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-28
 * @version [Android PABank C01, @2011-12-28]
 * @description 年月日 上午下午dialog
 */
public class YearMonthDayAmPmDialog extends BaseDialog implements
		OnWheelChangedListener, OnWheelScrollListener {

	/**
	 * 返回的结果年
	 */
	private int mYear = 0;
	/**
	 * 返回的结果 上午下午
	 */
	private int mAmpm = 0;
	/**
	 * 返回结果的月份
	 */
	private int mMonth = 0;
	/**
	 * 当前年
	 */
	private int curYear;
	/**
	 * 当前月
	 */
	private int curMonth;
	/**
	 * 当前天
	 */
	private int curDay;
	/**
	 * 上午 下午
	 */
	private int curAmpm;
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 月份滚动View
	 */
	private WheelView month;
	/**
	 * 年份滚动View
	 */
	private WheelView year;
	/**
	 * 天数滚动View
	 */
	private WheelView day;
	/**
	 * 上午下午滚动View
	 */
	private WheelView ampm;
	/**
	 * 日历
	 */
	private Calendar calendar;
	/**
	 * 返回view
	 */
	private View contentView;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param title
	 *            标题
	 */
	public YearMonthDayAmPmDialog(Context context, String title) {

		super(context);
		this.context = context;
		setTitle(title);
		initDate();

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化数据
	 */
	private void initDate() {
		calendar = CommonUtils.getCurrentTimeByCalendarAmpm();
		// 初始化View
		initView();
		// 添加监听器
		CalendarDialogCommon.addChangeListener(this, year, month, day, ampm);
		
		setBtn1ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (onYearMonthDayAmPmDialogScrollEndListener != null) {
					onYearMonthDayAmPmDialogScrollEndListener
							.onYearMonthDayAmPmDialogScrollEndListener(mYear
									+ "", CalendarDialogCommon
									.convertZeroString(mMonth),
									CalendarDialogCommon
											.convertZeroString(curDay),
									CalendarDialogCommon
											.convertZeroString(mAmpm));
				}

				YearMonthDayAmPmDialog.this.dismiss();
			}
		});

		setBtn2ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				YearMonthDayAmPmDialog.this.dismiss();
			}
		});

		// year
		curYear = calendar.get(Calendar.YEAR);
		year.setViewAdapter(new DateNumericAdapter(context, curYear,
				curYear + 50, 0, "年"));
		year.setCurrentItem(curYear);
		mYear = curYear;
		// month
		curMonth = calendar.get(Calendar.MONTH);
		month.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.MONTHS, curMonth));
		month.setCurrentItem(curMonth);
		mMonth = curMonth + 1;
		// day
		curDay = calendar.get(Calendar.DAY_OF_MONTH);
		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new DateNumericAdapter(context, 1, maxDays, calendar
				.get(Calendar.DAY_OF_MONTH) - 1, "日"));
		day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);

		// amppm
		curAmpm = calendar.get(Calendar.AM_PM);
		
		ampm.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.AM_PM, curAmpm));
		ampm.setCurrentItem(curAmpm);
		
		APPLog.i("zbkc", "curYear:"+curYear+"curMonth:"+curMonth+"curDay:"+curDay+"curAmpm:"+curAmpm);
	}
	
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param y 年
	 * @param m 月
	 * @param d 日
	 * @param am 上午下午
	 */
	 public void select(int y, int m, int d,int am) {
		 
		 APPLog.i("zbkc", "year:"+y+"month:"+m+"day:"+d);
		 month.setCurrentItem(m-1);
		 year.setCurrentItem(y-curYear);
		 day.setCurrentItem(d - 1);
		 ampm.setCurrentItem(am);
		 month.invalidate();
		 year.invalidate();
		 day.invalidate();
		 ampm.invalidate();
		 
   }

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化view
	 */
	private void initView() {

		contentView = LayoutInflater.from(getContext()).inflate(
				R.layout.app_widget_year_month_day_ampm_layout, null);

		month = (WheelView) contentView.findViewById(R.id.month);
		year = (WheelView) contentView.findViewById(R.id.year);
		day = (WheelView) contentView.findViewById(R.id.day);
		ampm = (WheelView) contentView.findViewById(R.id.ampm);
		year.addScrollingListener(this);
		month.addScrollingListener(this);
		day.addScrollingListener(this);
		ampm.addScrollingListener(this);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 滑动结束监听器
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2011-12-30]
	 */
	public interface OnYearMonthDayAmPmDialogScrollEndListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param year
		 *            年
		 * @param month
		 *            月
		 * @param day
		 *            日
		 * @param ampm
		 *            上午 下午
		 */
		void onYearMonthDayAmPmDialogScrollEndListener(String year,
				String month, String day, String ampm);

		void onScrollEnd(String year, String month, String day, String ampm,
				boolean scrollState);
	}

	/**
	 * 滑动结束监听器
	 */
	private OnYearMonthDayAmPmDialogScrollEndListener onYearMonthDayAmPmDialogScrollEndListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnYearMonthDayAmPmDialogScrollEndListener
	 */
	public OnYearMonthDayAmPmDialogScrollEndListener getOnYearMonthDayAmPmDialogScrollEndListener() {
		return onYearMonthDayAmPmDialogScrollEndListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onYearMonthDayAmPmDialogScrollEndListener
	 */
	public void setOnYearMonthDayAmPmDialogScrollEndListener(
			OnYearMonthDayAmPmDialogScrollEndListener onYearMonthDayAmPmDialogScrollEndListener) {
		this.onYearMonthDayAmPmDialogScrollEndListener = onYearMonthDayAmPmDialogScrollEndListener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {

		switch (wheel.getId()) {
		case R.id.year:
			mYear = curYear + newValue;
			/*if (newValue != 0) {
				CalendarDialogCommon.updateDays(context, year,curYear, month, day);
			}*/
			CalendarDialogCommon.updateDays(context, year,curYear, month, day);
			break;
		case R.id.month:
			mMonth = newValue + 1;
			/*if (newValue != calendar.get(Calendar.MONTH)) {
				CalendarDialogCommon.updateDays(context, year,curYear, month, day);
			}*/
			CalendarDialogCommon.updateDays(context, year,curYear, month, day);
			break;
		case R.id.day:
			curDay = newValue + 1;
			break;
		case R.id.ampm:
			mAmpm = newValue;
			break;
		default:
			break;
		}
		APPLog.i("zbkc", "newValue:" + newValue);

		APPLog.i("zbkc",
				mYear + "-" + CalendarDialogCommon.convertZeroString(mMonth)
						+ "-" + CalendarDialogCommon.convertZeroString(curDay)
						+ "-" + CalendarDialogCommon.convertZeroString(mAmpm));
	}

	@Override
	public View createContentView() {

		return contentView;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		if (onYearMonthDayAmPmDialogScrollEndListener != null) {
			onYearMonthDayAmPmDialogScrollEndListener.onScrollEnd(mYear + "",
					CalendarDialogCommon.convertZeroString(mMonth),
					CalendarDialogCommon.convertZeroString(curDay),
					CalendarDialogCommon.convertZeroString(mAmpm), false);
		}

		setBtn1Enable(false);
		setBtn2Enable(false);
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		if (onYearMonthDayAmPmDialogScrollEndListener != null) {
			onYearMonthDayAmPmDialogScrollEndListener.onScrollEnd(mYear + "",
					CalendarDialogCommon.convertZeroString(mMonth),
					CalendarDialogCommon.convertZeroString(curDay),
					CalendarDialogCommon.convertZeroString(mAmpm), true);
		}
		setBtn1Enable(true);
		setBtn2Enable(true);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void yearScroll(int position) {
		year.scroll(position, 0);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void monthScroll(int position) {
		month.scroll(position, 0);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void dayScroll(int position) {
		day.scroll(position, 0);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void ampmScroll(int position) {
		ampm.scroll(position, 0);
	}

}
